package cn.lizhangqu.kb.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.UnsupportedEncodingException;

import cn.lizhangqu.kb.R;
import cn.lizhangqu.kb.util.HttpUtil;
import cn.lizhangqu.kb.util.Toasts;

/**
 * Created by 柯尊诚 on 2015/11/5.
 * kzc
 */
public class EpActivity extends Activity{

    private EditText eField;
    private Button putBtn;
    private TextView resultTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_e);
        init();
    }

    private void init() {
        eField = (EditText) findViewById(R.id.et_e_query);
        putBtn = (Button) findViewById(R.id.btn_e_put);
        resultTv = (TextView) findViewById(R.id.tv_e_result);


        putBtn.setOnClickListener(dataOnClick);
    }

    private View.OnClickListener dataOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String cardCode = eField.getText().toString();
            if (cardCode.isEmpty()) {
                Toasts.makeText("请输入E通卡卡号");
                return;
            }
            HttpUtil.getEQuery(EpActivity.this, cardCode, new HttpUtil.QueryCallback() {
                @Override
                public String handleResult(byte[] result) {
                    try {
//                        resultTv.setText(Html.fromHtml(new String(result,"utf-8")));

                        Document doc = Jsoup.parse(new String(result,"utf-8"));
                        Elements elements = doc.select("div.card");
                        resultTv.setText(elements.text());
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    return null;
                }
            });

        }
    };
}
